<?php

################################################################################################

#  Visitor Counter Module for Opencart 1.5.1.x from HostJars http://opencart.hostjars.com      #

################################################################################################

class ModelModuleVvcounter extends Model {

	public function addVisitor($ip_address) {
		//$query = $this->db->query("INSERT INTO " . DB_PREFIX . "visitor_counter (prodid,word,count) VALUES (1,'" . $this->db->escape($ip_address) . "',1) ON DUPLICATE KEY UPDATE count=count+1");
		$query = "SELECT timelog FROM `".DB_PREFIX."vvcounter_logs` WHERE `ip_address`='$ip_address' ORDER BY timelog DESC LIMIT 1";		
		$r = $this->db->query($query);		
		if(count($r->rows)){
			$result = $r->row["timelog"];
			$k = strtotime("now") - strtotime($result);
			$t = ($k/3600)*60;
			if($t>15){
				$query = "INSERT INTO `".DB_PREFIX."vvcounter_logs` (`ip_address`, `timelog`) VALUES ('".$this->db->escape($ip_address)."', ".date("YmdHis").")";
				$this->db->query($query);
			}
		}

	}

	public function getTotalvisitors() {

		$query = $this->db->query("SELECT `ip_address` as word , count(*) as count FROM " . DB_PREFIX . "vvcounter_logs GROUP BY `ip_address`");
		return $query->rows;	

	}
	
	public function getVisitorsFromLog($timestart=0, $timestop=0){
		$query = "SELECT `ip_address` as word , count(*) as count FROM " . DB_PREFIX . "vvcounter_logs WHERE 1=1 ";
		$total = array();
		$total["ip_addrress"] = 0;
		$total["count"] = 0;		
		if ( !$timestart ){
			if ( $timestop ) {
				$query	.=	" AND timelog <= '$timestop' ";
			}
		}
		else{
			if ( !$timestop ){
				$query		.= " AND timelog > '$timestart' ";
			}
			else{
					$query		.= " AND timelog > '$timestart' ";
					$query		.= " AND timelog <= '$timestop' ";										
			}
		}
		
		$query .= " GROUP BY `ip_address`";
		return $query;		
	}
	
	public function getTotalguest($timestart=0, $timestop=0){
		$query = $this->getVisitorsFromLog($timestart, $timestop);
		//echo $query . "<br>";
		$r = $this->db->query($query);
		return $r->rows;
		
	}
}



?>